package com.jt.controller;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.jt.pojo.User;
import com.jt.service.UserService;
import com.jt.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisCluster;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private JedisCluster jedisCluster;

    @RequestMapping("/getMsg")
    public String getMsg(){

        return "单点登录系统测试完成";
    }

    /**
     *  JSONP
     * 实现用户数据校验
     * url:http://sso.jt.com/user/check/{param}/{type}
     * 参数: /{param} 用户需要校验的数据
     *      /{type}   校验的字段.
     * 返回值: SysResult对象(true/false)
     */
    @RequestMapping("/check/{param}/{type}")
    public JSONPObject checkUser(@PathVariable String param,
                                 @PathVariable Integer type,
                                 String callback){

        //查询数据库获取响应信息.
        boolean flag = userService.checkUser(param,type);
        SysResult sysResult = SysResult.success(flag);
        return new JSONPObject(callback, sysResult);
        //callback(JSON结构)
    }

    /**
     * 完成HttpClient业务调用
     * url地址: http://sso.jt.com/user/findUserList
     * 返回值: UserJSON
     */
    @RequestMapping("/findUserList")
    public List<User> findUserList(){

        return userService.findUserList();
    }

    /**
     * 跨域请求:完成用户信息获取
     * URL网址:  http://sso.jt.com/user/query/dca70b16a1c54aea9ebb0b27621250de?callback=jsonp1608021961735&_=1608021961777
     * 参数:    参数1: ticket信息    参数2:callback
     * 返回值:   SysResult对象(用户数据......)
     */
    @RequestMapping("/query/{ticket}")
    public JSONPObject findUserByTicket(@PathVariable String ticket,
                                        String callback){
        //如何获取用户信息?  从redis中获取数据
        if(jedisCluster.exists(ticket)){
            String userJSON = jedisCluster.get(ticket);
            SysResult sysResult = SysResult.success(userJSON);
            return new JSONPObject(callback, sysResult);
        }else{
            return new JSONPObject(callback, SysResult.fail());
        }
    }

















}
